package com.itheima.demo4.ZengDemo4;

import java.util.List;
import java.util.Random;

/**
 * @ClassName peopleThread
 * @Author 16799
 * @Date 2025/9/29 21:19
 * @Version 1.0
 * @Description TODO
 **/
public class peopleThread extends Thread{
    private List<Integer> moneys;
    private int totalMoney;
    private Random r = new Random();

    public peopleThread(List<Integer> moneys, String name) {
        super(name);
        this.moneys = moneys;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        while (true){
            String name = Thread.currentThread().getName();
            synchronized (moneys){
                if (moneys.size()==0){
                    break;
                }
                int money = moneys.remove(r.nextInt(moneys.size()));
                System.out.println(name+"线程抢到了： "+money);
                totalMoney+=money;

                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public int getTotalMoney() {
        return totalMoney;
    }

    public void setTotalMoney(int totalMoney) {
        this.totalMoney = totalMoney;
    }
}
